package hl.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import hl.dao.BaseDao;
import hl.model.TPlace;
import hl.model.TVenueInfo;
import hl.pageModel.DataGrid;
import hl.pageModel.Place;
import hl.service.PlaceService;

@Service("placeService")
public class PlaceServiceImpl implements PlaceService {

	private BaseDao<TPlace> placeDao;

	public BaseDao<TPlace> getPlaceDao() {
		return placeDao;
	}

	private BaseDao<TVenueInfo> venueDao;

	public BaseDao<TVenueInfo> getVenueDao() {
		return venueDao;
	}
	@Autowired
	public void setVenueDao(BaseDao<TVenueInfo> venueDao) {
		this.venueDao = venueDao;
	}
	@Autowired
	public void setPlaceDao(BaseDao<TPlace> placeDao) {
		this.placeDao = placeDao;
	}

	
	@Override
	public DataGrid getDatagrid(Place place) {
		DataGrid dg = new DataGrid();
		String hql = "from TPlace t ";

		Map<String, Object> params = new HashMap<String, Object>();

		//hql = search(place, hql, params);

		if (place.getOrder() != null) {
			hql += " order by " + place.getSort() + " " + place.getOrder();
		}

		List<TPlace> l = placeDao.find(hql, params, place.getPage(), place.getRows());

		int totalHql = placeDao.count(hql, params);

		List<Place> pl = new ArrayList();
		if (l != null && l.size() > 0) {
			for (TPlace t : l) {
				Place u = new Place();
				BeanUtils.copyProperties(t, u);
				u.setPkVenueInfo(t.getTVenueInfo().getPkVenueInfo());
				u.setVenueName(t.getTVenueInfo().getName());
				pl.add(u);
			}
		}
		dg.setRows(pl);
		dg.setTotal(totalHql);
		return dg;
	}

	private String search(Place place, String hql, Map<String, Object> params) {
		/*if (place.getName() != null && !place.getName().trim().equals("")) {
			hql += " where t.name like :value1 ";
			params.put("value1", "%" + place.getName().trim() + "%");
		}

		if (place.getGender() != null && !place.getGender().equals("")) {
			if (hql.contains("where"))
				hql += " and ";
			else
				hql += " where ";
			hql += " t.gender like :value2 ";
			params.put("value2", "%" + place.getGender().trim() + "%");
		}
		if (place.getPkplace() != null && !place.getPkplace().equals("")) {
			if (hql.contains("where"))
				hql += " and ";
			else
				hql += " where ";
			hql += " t.pkplace like :value3 ";
			params.put("value3", "%" + place.getPkplace().toString() + "%");
		}*/
		return hql;
	}

	@Override
	public void save(Place place) {
		TPlace t = new TPlace();
		BeanUtils.copyProperties(place, t);
		t.setCdatetime(new Date());
		t.setTVenueInfo(venueDao.get(TVenueInfo.class, place.getPkVenueInfo()));
		placeDao.save(t);
	}

	@Override
	public void edit(Place place) {
		TPlace t = new TPlace();
		BeanUtils.copyProperties(place, t);
		t.setMdatetime(new Date());
		placeDao.saveOrUpdate(t);
	}

	@Override
	public void remove(Place place) {
		TPlace t = placeDao.get(TPlace.class, place.getPkPlace());
		placeDao.delete(t);
	}

	@Override
	public Object getComboBox(Place place) {
		List<TPlace> l = placeDao.find("from TPlace");
		List<Place> pl = new ArrayList();
		if (l != null && l.size() > 0) {
			for (TPlace t : l) {
				Place u = new Place();
				BeanUtils.copyProperties(t, u);
				pl.add(u);
			}
		}
		return pl;
	}

}
